//package com.example.administrator.computerplaza;
//
//import android.content.Context;
//import android.graphics.Bitmap;
//import android.graphics.Canvas;
//import android.graphics.Paint;
//import android.graphics.PorterDuff;
//import android.graphics.PorterDuffXfermode;
//import android.graphics.Rect;
//import android.graphics.RectF;
//import android.graphics.drawable.BitmapDrawable;
//import android.graphics.drawable.Drawable;
//import android.support.v7.widget.AppCompatImageView;
//import android.util.AttributeSet;
//
//public class MakeRoundImage extends AppCompatImageView {
//    Paint paint;
//    public MakeRoundImage(Context context) {
//        super(context);
//    }
//
//    public MakeRoundImage(Context context, AttributeSet attrs) {
//        super(context, attrs);
//    }
//
//    public MakeRoundImage(Context context, AttributeSet attrs, int defStyleAttr) {
//        super(context, attrs, defStyleAttr);
//    }
//
//    @Override
//    protected void onDraw(Canvas canvas) {
//        Drawable drawable = getDrawable();
//        if (drawable != null) {
//            Bitmap bitmap = ((BitmapDrawable) drawable). getBitmap();
//            Bitmap b = getRoundRectBitmap(bitmap);
//            /**
//             * rectScr 是对图片进行裁截，若是空null则显示整个图片
//             rectdest ：是图片在Canvas画布中显示的区域，
//             */
//            final Rect rectScr = new Rect(0, 0, b.getWidth(),b.getHeight());
//            final Rect rectdest = new Rect(0, 0, getWidth(), getHeight());
//            paint.reset();//画笔重置
//            canvas.drawBitmap(b, rectScr, rectdest, paint);//在原有的画布中剪裁出图片
//        } else {
//            super.onDraw(canvas);
//        }
//    }
//
//    /**
//     *
//     * @param bitmap
//     * @return 实现矩形图片圆形化
//     */
//    private Bitmap getRoundRectBitmap(Bitmap bitmap) {
//        int height = bitmap.getHeight();
//        int width = bitmap.getWidth();
//        int radis = height > width ? width : height;//取最短的边长作为边长
//
//        Bitmap background = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
//        Canvas canvas = new Canvas(background);//画板
//
//        paint = new Paint();
//        paint.setAntiAlias(true);
//        RectF rect = new RectF(0, 0, radis, radis);
//        canvas.drawRoundRect(rect,radis >> 1, radis >> 1, paint);
//        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
//        canvas.drawBitmap(bitmap,null, rect, paint);
//        return  background;//返回画好的
//    }
//}

package com.example.zwh.computershop;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

public class MakeRoundImage extends android.support.v7.widget.AppCompatImageView {

    //画笔
    private Paint mPaint;
    //圆形图片的半径
    private int mRadius;
    //图片的宿放比例
    private float mScale;

    public MakeRoundImage(Context context) {
        super(context);
    }

    public MakeRoundImage(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MakeRoundImage(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //由于是圆形，宽高应保持一致
        int size = Math.min(getMeasuredWidth(), getMeasuredHeight());
        mRadius = size / 2;
        setMeasuredDimension(size, size);
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {

        mPaint = new Paint();

        Drawable drawable = getDrawable();

        if (null != drawable) {
            Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();

            //初始化BitmapShader，传入bitmap对象
            BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            //计算缩放比例
            mScale = (mRadius * 2.0f) / Math.min(bitmap.getHeight(), bitmap.getWidth());

            Matrix matrix = new Matrix();
            matrix.setScale(mScale, mScale);
            bitmapShader.setLocalMatrix(matrix);
            mPaint.setShader(bitmapShader);
            //画圆形，指定好坐标，半径，画笔
            canvas.drawCircle(mRadius, mRadius, mRadius, mPaint);
        } else {
            super.onDraw(canvas);
        }
    }

}

